<?php

namespace app\api\service;

use app\common\model\Config;
use think\Log;
use WeMeetingGateWay\WeMeetingApi\WeMeetingApi;
use WeMeetingGateWay\Credential;
use WeMeetingGateWay\Exception\WeMeetingException;

//腾讯会议
class TengxunSignatureUtil
{
    private $config;
    private $app;

    public function __construct()
    {
        $this->config = Config::where('group', 'live_config')->field('name,title,value')->column('value', 'name');
        // 测试时请将以下参数替换成有效参数
        $secret_id = $this->config['tengxunmeet_secret_id']; // SecretID
        $secret_key = $this->config['tengxunmeet_secret_key']; // SecretKey
        $AppId = $this->config['tengxunmeet_app_id']; // appid
        $sdk_id = $this->config['tengxunmeet_sdk_id'];
        $userid = $this->config['tengxunmeet_username'];
        $username = $this->config['tengxunmeet_username'];
        $cred = new Credential($secret_id, $secret_key);
        $meeting_api = new WeMeetingApi($cred, $AppId, $userid);
        $instanceid = 1; // 用户的终端设备类型
        $this->app = $meeting_api->setInstanceId($instanceid)
            ->setCommonHeader(['SdkId' => $sdk_id]);  // 设置公共参数，如果没有SdkId 可不设置
    }

    /**
     * 获得初始化用户
     * @return mixed|string
     */
    public function getInitUser()
    {
        try {
            // 查询会议，需要会议id或者会议Code参数
            $meeting_info = $this->app->getUserList(1, 10);
            $userList = $meeting_info['users'];
            $info_index = array_search('ADMIN_ROLE', array_column($userList, 'role_code'));
            return $userList[$info_index]['userid'] ?? '';
        } catch (WeMeetingException $e) {
            Log::error(print_r($e, true));
            return $e->getMessage();
        }
    }

    /**
     * 创建会议
     */
    public function createMeeting($title, $type = 1)
    {
        try {
            $params['subject'] = date('Y-m-d H:i:s') . $title;
            $params['start_time'] = (string)time();
            $endtime = time() + 86400;
            $params['end_time'] = (string)$endtime;
            $params['type'] = $type;
            $params['enable_live'] = true;
            $params['live_config'] = [
                "live_subject" => "复盘直播",
                "live_summary" => "复盘直播",
                "enable_live_password" => false,
                "enable_live_im" => false,
                "enable_live_replay" => false,
                "live_watermark" => [
                    "watermark_opt" => 0
                ]
            ];
            $meeting_info = $this->app->createMeeting($params);
            return $meeting_info;
        } catch (WeMeetingException $e) {
            Log::error(print_r($e, true));
            return $e->getMessage();
        }
    }

    public function setliveStreamAddress($meetingId, $push_address, $push_password)
    {
        try {
            return $this->app->liveStreamAddress($meetingId, $push_address, $push_password);
        } catch (WeMeetingException $e) {
            Log::error(print_r($e, true));
            return $e->getMessage();
        }
    }

    public function inviteActivate()
    {
        try {
            return $this->app->inviteActivate();
        } catch (WeMeetingException $e) {
            Log::error(print_r($e, true));
            return $e->getMessage();
        }
    }

    public function updateLivePlay($meeting_id, $meeting_code, $live_config)
    {
        try {
            return $this->app->updateLivePlay($meeting_id, $meeting_code, $live_config);
        } catch (WeMeetingException $e) {
            Log::error(print_r($e, true));
            return $e->getMessage();
        }
    }

    /**
     * 获取会议录屏内容
     * @param $meeting_info | 直播会议的meet_info字段数据
     * @return mixed|string|null
     */
    public function getLiveReplays($meet_info)
    {
        try {
            $meeting_id = $meet_info['meeting_info_list'][0]['meeting_id'];
            $meeting_code = $meet_info['meeting_info_list'][0]['meeting_code'];
            $getLiveReplays = $this->app->getLiveReplays($meeting_id, $meeting_code);
            $data = [];
            if (isset($getLiveReplays['total_page']) && $getLiveReplays['total_page']) {
                if ($getLiveReplays['current_page'] == $getLiveReplays['total_page']) {
                    foreach ($getLiveReplays['record_meetings'] as $item) {
                        $data[] = $item;
                    }
                } else {
                    for ($i = 2; $i <= $getLiveReplays['total_page']; $i++) {
                        $getLiveReplays = $this->app->getLiveReplays($meeting_id, $meeting_code, $i);
                        foreach ($getLiveReplays['record_meetings'] as $item) {
                            $data[] = $item;
                        }
                    }

                }
            }
            return $data;
        } catch (WeMeetingException $e) {
            Log::error(print_r($e, true));
            return $e->getMessage();
        }
    }


    /**
     * 查询会议录制地址信息
     * @param $meeting_record_id
     * @return array|string
     */
    public function getReplaysInfo($meeting_record_id)
    {
        try {
            $data = $this->app->getReplaysInfo($meeting_record_id);//会议录制 ID
            return $data;
        } catch (WeMeetingException $e) {
            Log::error(print_r($e, true));
            return $e->getMessage();
        }
    }
}